﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;

/// <summary>
/// active 的摘要说明
/// </summary>
public class active
{
    public static string Conn = getsql.getstr("activestr");
    //public static string Conn = "server=.;User ID='sa';Password='wuxinjie';Pooling=true;Max Pool Size=30;database=TenTiange;Connection Reset=FALSE";
    //public static string Conn2 = "server=122.227.23.112,3433;User ID='tenroom';Password='tenroom';Pooling=true;Max Pool Size=30;database=TenRoomTest;Connection Reset=FALSE";
    private static System.Web.Caching.Cache cc = HttpRuntime.Cache;
	public active()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public static void accessControl(string name)
    {
        int useridx = MediaUtil.ReturnMemberId();
        string userid = MediaUtil.ReturnUserID();

        //useridx = 18464630;
        //userid = "wuxinjie0515";

        if (useridx <= 0)
        {
            HttpContext.Current.Response.Redirect("http://my.9158.com/login_pass.aspx?go=http://cugame.9158.com/active/"+name+"/index.aspx");
            HttpContext.Current.Response.End();
        }

    }

    /// <summary>
    /// 生成json格式
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string CreateJson(DataTable dt)
    {
        StringBuilder JsonString = new StringBuilder();
        //Exception Handling       
        if (dt != null && dt.Rows.Count > 0)
        {
            JsonString.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                JsonString.Append("{ ");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (j < dt.Columns.Count - 1)
                    {
                        JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                    }
                    else if (j == dt.Columns.Count - 1)
                    {
                        JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                    }
                }
                //end Of String
                if (i == dt.Rows.Count - 1)
                {
                    JsonString.Append("} ");
                }
                else
                {
                    JsonString.Append("}, ");
                }
            }
            JsonString.Append("]");
            return JsonString.ToString();
        }
        else
        {
            return null;
        }
    }


    /// <summary>
    /// 查询个人信息
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static DataTable UserInfo(int useridx)
    {
        DataTable dt = new DataTable();

        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeOutParam("@num",SqlDbType.Int,4),
                         SqlHelper.MakeOutParam("@num2",SqlDbType.Int,4),
                         SqlHelper.MakeOutParam("@num3",SqlDbType.Int,4),
                         SqlHelper.MakeOutParam("@text",SqlDbType.VarChar,200),
                    };
        try
        {

            dt = SqlHelper.ExecuteDataTable(Conn, CommandType.StoredProcedure, "[active_UserInfo]", p);
        }
        catch
        {

        }
        return dt;
    }



     /// <summary>
    /// 投票
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static DataTable active_vote(int type,int useridx,int toidx,int RaceType,int RaceOrder,out int ret)
    {
        DataTable dt = new DataTable();
        ret = -2;
        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                         SqlHelper.MakeInParam("@idx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@toidx",SqlDbType.Int,4,toidx),
                         SqlHelper.MakeInParam("@RaceType",SqlDbType.Int,4,RaceType),
                         SqlHelper.MakeInParam("@RaceOrder",SqlDbType.Int,4,RaceOrder),
                         SqlHelper.MakeOutParam("@return",SqlDbType.Int,4)
                    };
        try
        {
            dt = SqlHelper.ExecuteDataTable(Conn, CommandType.StoredProcedure, "[active_vote_f]", p);
            ret = int.Parse(p[5].Value.ToString());
        }
        catch
        {

        }
        return dt;
    }


    /// <summary>
    /// 查询排行榜
    /// </summary>
    /// <returns></returns>
    public static DataSet List(int type)
    {
        DataSet ds = null;
        if (cc["RankList"] == null)
        {

            SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                                  
                    };
            try
            {

                ds = SqlHelper.ExecuteDataset(Conn, CommandType.StoredProcedure, "[activegiftrank]", p);
                if(ds!=null && ds.Tables[0].Rows.Count>0)
                cc.Insert("RankList", ds, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero);
            }
            catch
            {

            }
        }
        else
        {
            ds = (DataSet)cc["RankList"];
        }

        return ds;
    }


    /// <summary>
    /// 查询排行榜
    /// </summary>
    /// <returns></returns>
    public static DataSet ListV2(int type)
    {
        DataSet ds = null;
        if (cc["ListV2"] == null)
        {

            SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                                  
                    };
            try
            {

                ds = SqlHelper.ExecuteDataset(Conn, CommandType.StoredProcedure, "[activegiftrank]", p);
                cc.Insert("ListV2", ds, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero);
            }
            catch
            {

            }
        }
        else
        {
            ds = (DataSet)cc["ListV2"];
        }

        return ds;
    }


    /// <summary>
    ///  升级
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static string UpdateLevel(int useridx, string userid)
    {
       
        string output = string.Empty;
        int state = -2;

        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@userid",SqlDbType.VarChar,30,userid),
                         SqlHelper.MakeInParam("@Rank",SqlDbType.Int,4,10),
                         SqlHelper.MakeOutParam("@ret",SqlDbType.Int,4)
                    };
        try
        {

            SqlHelper.ExecuteNonQuery(Conn, CommandType.StoredProcedure, "[active_UpdateLevel]", p);
            state = int.Parse(p[3].Value.ToString());
        }
        catch
        {

        }

        output = state.ToString();
        return output;
    }


    /// <summary>
    ///  留言
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static string InsertMessage(int useridx, string message)
    {
        
        string output = string.Empty;
        int state = -2;

        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@message",SqlDbType.VarChar,200,message),
                         SqlHelper.MakeOutParam("@ret",SqlDbType.Int,4)
                    };
        try
        {

            SqlHelper.ExecuteNonQuery(Conn, CommandType.StoredProcedure, "[active_InsertMessage]", p);
            state = int.Parse(p[2].Value.ToString());
        }
        catch
        {

        }

        output = state.ToString();
        return output;
    }

    /// <summary>
    /// 查询留言信息
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static DataTable GetMessage(int page,int pagesize)
    {
        DataTable dt = new DataTable();


        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,1),
                         SqlHelper.MakeInParam("@id",SqlDbType.Int,4,0),
                         SqlHelper.MakeInParam("@page",SqlDbType.Int,4,page),
                        SqlHelper.MakeInParam("@pagesize",SqlDbType.Int,4,pagesize)
                    };
        try
        {
            dt = SqlHelper.ExecuteDataTable(Conn, CommandType.StoredProcedure, "[active_selectmessage]", p);
        }
        catch
        {

        }
        return dt;
    }


    /// <summary>
    /// 查询留言信息
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static DataTable Message_Get(int type, int useridx, int page, int pagesize, out int count)
    {
        DataTable dt = new DataTable();
        count = 0;
        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@page",SqlDbType.Int,4,page),
                         SqlHelper.MakeInParam("@pagesize",SqlDbType.Int,4,pagesize),
                         SqlHelper.MakeOutParam("@return",SqlDbType.Int,4)
                    };
        try
        {
            dt = SqlHelper.ExecuteDataTable(Conn, CommandType.StoredProcedure, "[active_message_GET]", p);
            count = int.Parse(p[4].Value.ToString());
        }
        catch
        {

        }
        return dt;
    }

    /// <summary>
    ///  留言
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public static string Message_Insert(int type, long id, int useridx, string message, string imageurl, int ispass)
    {

        string output = string.Empty;
        int state = -2;

        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@MessageType",SqlDbType.Int,4,type),
                         SqlHelper.MakeInParam("@id",SqlDbType.BigInt,15,id),
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@message",SqlDbType.VarChar,600,message),
                         SqlHelper.MakeInParam("@imageurl",SqlDbType.VarChar,50,imageurl),
                         SqlHelper.MakeInParam("@IsPass",SqlDbType.Int,4,ispass),
                         SqlHelper.MakeOutParam("@return",SqlDbType.Int,4)
                    };
        try
        {

            SqlHelper.ExecuteNonQuery(Conn, CommandType.StoredProcedure, "[active_message_Insert]", p);
            state = int.Parse(p[6].Value.ToString());
        }
        catch
        {

        }

        output = state.ToString();
        return output;
    }


}
